package yunjiao.javatutorials.designpatterns.combinator;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * {@link Before} 示例
 *
 * @author yangyunjiao
 */
@Slf4j
public class BeforeExample {
    void demo() {
        log.info("----------------------------------");
        log.info("Starting BEFORE combinator demo...");
        log.info("----------------------------------");

        Function<BigDecimal, String> addTax = this::addTax;

        Consumer<BigDecimal> before = this::before;

        Function<BigDecimal, String> addTaxDecorated =
                Before.decorate(before, addTax);

        BigDecimal argument = new BigDecimal("100");
        String result = addTaxDecorated.apply(argument);

        log.info("Done - Result is {}", result);
    }

    private void before(BigDecimal argument) {
        log.info("BEFORE: Argument is " + argument);
    }

    private String addTax(BigDecimal amount) {
        log.info("Adding heavy taxes to poor citizen...");
        return "$" + amount.multiply(new BigDecimal("1.22"));
    }
}
